//
//  OBMomentProfileView.m
//  WeChat
//
//  Created by 黄柳姣 on 2018/5/2.
//  Copyright © 2018年 obally. All rights reserved.
//

#import "OBMomentProfileView.h"
#import "OBControl.h"
#import "OBMomentProfileViewModel.h"

@interface OBMomentProfileView()
/// 昵称
@property (nonatomic, readwrite, weak) YYLabel *screenNameLable ;

/// 封面
@property (nonatomic, readwrite, weak) OBControl *coverBtn;

/// 头像
@property (nonatomic, readwrite, weak) OBControl *avatarView;
/// viewModel
@property (nonatomic, readwrite, strong) OBMomentProfileViewModel *viewModel;
@end
@implementation OBMomentProfileView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //初始化
        [self _setUp];
        ///创建视图
        [self _setupSubViews];
        ///布局子控件
        [self _makeSubViewsConstraints];
    }
    return self;
}

- (void)bindViewModel:(OBMomentProfileViewModel *)viewModel
{
    self.viewModel = viewModel;
    ///封面
    [self.coverBtn.layer setImageWithURL:[NSURL URLWithString:viewModel.user.profileImage]   placeholder:OBDefaultAvatar(OBDefaultAvatarTypeDefualt) options:YYWebImageOptionAllowInvalidSSLCertificates|YYWebImageOptionAllowBackgroundTask|YYWebImageOptionAvoidSetImage completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
        if (image && stage == YYWebImageStageFinished) self.avatarView.image = image;
    }];
    ///昵称
    self.screenNameLable.attributedText = viewModel.screenNameAttr;
    ///头像
    [self.avatarView.layer setImageWithURL:[NSURL URLWithString:viewModel.user.avatar]   placeholder:OBDefaultAvatar(OBDefaultAvatarTypeDefualt) options:YYWebImageOptionAllowInvalidSSLCertificates|YYWebImageOptionAllowBackgroundTask|YYWebImageOptionAvoidSetImage completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
        if (image && stage == YYWebImageStageFinished) self.avatarView.image = image;
    }];
}
///初始化
- (void)_setUp
{
    self.backgroundColor = [UIColor whiteColor];
}
///创建视图
- (void)_setupSubViews
{
    ///封面
    OBControl *coverBtn = [[OBControl alloc]init];
    self.coverBtn = coverBtn;
    [self addSubview:coverBtn];
    ///昵称
    YYLabel *nickNameLable = [[YYLabel alloc]init];
    nickNameLable.backgroundColor = [UIColor clearColor];
    nickNameLable.textAlignment = NSTextAlignmentRight;
    nickNameLable.displaysAsynchronously = NO;
     /// 利用textLayout来设置text、font、textColor...
    nickNameLable.ignoreCommonProperties = NO;
    nickNameLable.fadeOnAsynchronouslyDisplay = NO;
    nickNameLable.fadeOnHighlight = NO;
    self.screenNameLable = nickNameLable;
    [coverBtn addSubview:nickNameLable];
    ///头像
    OBControl *avatarView = [[OBControl alloc]init];
    avatarView.layer.borderColor = OBColorFromHexString(@"#D4D4D4").CGColor;
    avatarView.layer.borderWidth = OBGlobalBottomLineHeight;
    self.avatarView = avatarView;
    [self addSubview:avatarView];
    
}
///布局子控件
- (void)_makeSubViewsConstraints
{
    ///布局封面
    [self.coverBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.and.top.equalTo(self);
        make.height.equalTo(self.coverBtn.mas_width);
    }];
    ///布局头像
    [self.avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.and.height.mas_equalTo(OBMomentProfileViewAvatarViewWH);
        make.right.equalTo(self).with.offset(-OBMomentContentInnerMargin);
        make.top.equalTo(self.coverBtn.mas_bottom).with.offset(24-OBMomentProfileViewAvatarViewWH);
    }];
    ///布局昵称
    [self.screenNameLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.coverBtn).with.offset(-OBMomentContentInnerMargin);    make.right.equalTo(self.avatarView.mas_left).with.offset(-OBMomentCommentViewContentLeftOrRightInset);
        make.left.equalTo(self.coverBtn);
    }];
}
@end
